package gogo

import (
	"strconv"
)

type UtilsIPAddressComponentStruct struct {
	Country         string `json:"country"`
	CountryCode     int    `json:"country_code"`
	CountryCodeISO  string `json:"country_code_iso"`
	CountryCodeISO2 string `json:"country_code_iso2"`
	Province        string `json:"province"`
	City            string `json:"city"`
	CityLevel       int    `json:"city_level"`
	District        string `json:"district"`
	Town            string `json:"town"`
	TownCode        string `json:"town_code"`
	Adcode          string `json:"adcode"`
	Street          string `json:"street"`
	StreetNumber    string `json:"street_number"`
	Direction       string `json:"direction"`
	Distance        string `json:"distance"`
}

type UtilsIPLocationStrucrt struct {
	Lng float64 `bson:"lng" json:"lng"`
	Lat float64 `bson:"lng" json:"lat"`
}

type UtilsIPResultStrucrt struct {
	Location          UtilsIPLocationStrucrt        `bson:"location" json:"location"`
	Formatted_address string                        `bson:"formatted_address" json:"formatted_address"`
	Business          string                        `bson:"business" json:"business"`
	AddressComponent  UtilsIPAddressComponentStruct `bson:"addressComponent" json:"addressComponent"`
	CityCode          int64                         `bson:"cityCode" json:"cityCode"`
}

type UtilsIPJSBaiduGPSInfo struct {
	Status  int                  `bson:"status" json:"status"`
	Result  UtilsIPResultStrucrt `bson:"result" json:"result"`
	Message string               `bson:"message" json:"message"`
}

const (
	UtilsIPBaiduCoordtypeBaidu = 1
	UtilsIPBaiduCoordtypeGPS   = 2
)

func UtilsIPGetBaduReverseGeocoding(ak string, longitude, latitude float64, coordtype int) (baiduGPSInfo UtilsIPJSBaiduGPSInfo) {
	if ak == "" {
		ak = "kKmKHGFeH7ooQtZx7Gnzhn9dEv3tGPR7"
	}

	uri := "https://api.map.baidu.com/reverse_geocoding/v3/?ak=" + ak + "&output=json&coordtype="

	slatitude := strconv.FormatFloat(latitude, 'f', 6, 64)
	slongitude := strconv.FormatFloat(longitude, 'f', 6, 64)

	if coordtype == UtilsIPBaiduCoordtypeBaidu {
		uri += "bd09ll&location=" + slatitude + "," + slongitude
	} else if coordtype == UtilsIPBaiduCoordtypeGPS {
		uri += "wgs84ll&location=" + slatitude + "," + slongitude
	}

	UtilsHttpClient().Get(uri).SetSuccessResult(&baiduGPSInfo).Do()
	return
}
